﻿define([
    'views/baseView',
  'models/crud/companyModel',
    'text!templates/crud/mngBase.html',
    'text!templates/crud/mngTable.html',
    'text!templates/crud/company/companyList.html',
], function (BaseView, Model, BaseTmp, TableTmp, UniqTmp) {
    var view = BaseView.extend({
        
        $content: null,
        $table: null,

        render: function (element) {
            toggleLoading(true);
            $("div[data-rel='crud_group']").remove();
            $("div[data-rel='crud_rol']").remove();
            $("div[data-rel='crud_company']").remove();
            var that = this;

            changeTopNavTitle("_companies");

            var baseTemplate = Handlebars.compile(BaseTmp);
            var tableTemplate = Handlebars.compile(TableTmp);

            var dataBaseOptions = {
                uuId: that._uId,
                dataRel: "crud_company", //
                tmpClass: "company_manager", // 
                title: translate("_companyManager"),
                subtitle: translate("")
            };

            var dataTableOptions = {
                column: [{
                    colText: translate("_name"),
                    style: "width: 279px;",
                    ariasort: true,
                    ariaLabel: "_nameOfTheCompany"
                }, {
                    colText: translate("_code"),
                    style: "width: 350px;",
                    ariasort: true,
                    ariaLabel: "_codeOfTheCompany"
                }, {
                    colText: translate("_system"),
                    style: "width: 322px;",
                    ariasort: true,
                    ariaLabel: "_isSystemCompany"
                }, {
                    colText: translate("_rols"),
                    style: "width: 241px;",
                    ariasort: false,
                    ariaLabel: "_rolOptions"
                }, {
                    colText: translate("_groups"),
                    style: "width: 241px;",
                    ariasort: false,
                    ariaLabel: "_groupsOptions"
                }, {
                    colText: translate("_options"),
                    style: "width: 241px;",
                    ariasort: false,
                    ariaLabel: "_generalOptions"
                }]
            };

            var html = baseTemplate(dataBaseOptions);
            var html2 = tableTemplate(dataTableOptions);

            (new Model()).listFull(function (oData) {
                var uniqTemplate = Handlebars.compile(UniqTmp);


                var dataUniqOptions = {
                    data: oData
                };

                var html3 = uniqTemplate(dataUniqOptions);
                
                that.$el.append(html);
                that.$el.find(".table_container").append(html2);
                that.$el.find("tbody.crud_tbody_content").append(html3);

                that.$content = $("div[data-id='" + that._uId + "']");
                that.$table = that.$content.find("table");
                postRender(that.$el);

                that.$table.customTable();

                that.addTableHandlers();
                that.addHandlers();

                toggleLoading(false);
            });
        },
        edit: function (e) {
            var id = $(e.currentTarget).closest("tr").attr("data-id");

            require(['controllers/abmManagers/companyManager'], function (Controller) {
                (new Controller()).showEdit(id);
            });
            
        },
        editPreferences: function(e){
            var id = $(e.currentTarget).closest("tr").attr("data-id");

            require(['controllers/abmManagers/companyManager'], function (Controller) {
                (new Controller()).showEditPref(id);
            });
        },
        deleteCompany: function (e) {
            var id = $(e.currentTarget).closest("tr").attr("data-id");
            
            require(['controllers/common/showModal'], function (Controller) {
                (new Controller()).show(
                    {
                        title: translate("_deleteCompany"),
                        text: translate("_sure_delete_company"),
                        buttons: [
                            { "title": translate("yes"), "css": "primary", "code": "yes" },
                            { "title": translate("no"), "code": "no" }
                        ],
                        callback: function (code) {
                            if (code == "yes") {
                                var model = new cmpyModel();
                                $("#mainLoading").show();
                                model.DeleteCompany(id, function () {
                                    $("#mainLoading").hide();
                                    require(['controllers/abmManagers/companyManager'], function (Controller) {
                                        (new Controller()).show();
                                    });
                                });
                            }
                        }
                    });
            });
        },
        addCompany: function () {
            require(['controllers/abmManagers/companyManager'], function (Controller) {
                (new Controller()).addCompany();
            });
        },
        mngRols: function (element, companyId) {
            require(['controllers/crud/rolMngr'], function (Controller) {
                (new Controller()).show(element, companyId);
            });
        },
        mngGroups: function (element, companyId) {
            require(['controllers/crud/groupMngr'], function (Controller) {
                (new Controller()).show(element, companyId);
            });
        },
        close: function () {
            var key = $("#currentTask").attr("data-rel");
            require(['controllers/home/home'], function (Controller) {
                (new Controller()).removeTask(key);
            });
        },
        addTableHandlers: function () {

            this.$table.find("a.btn_edit_rols").on("click", $.proxy(function (e) {
                this.mngRols(this.$el, +$(e.currentTarget).closest("tr").attr("data-id"));
                return false;
            }, this));
            this.$table.find("a.btn_edit_groups").on("click", $.proxy(function (e) {
                this.mngGroups(this.$el, +$(e.currentTarget).closest("tr").attr("data-id"));
                return false;
            }, this));
            this.$table.find("button.btn_edit_groups").on("click", this.edit);
            this.$table.find("button.btn_edit").on("click", this.edit);
            this.$table.find("button.btn-preference").on("click", this.editPreferences);
            this.$table.find("button.btn-delete").on("click", this.deleteCompany);
        },
        addHandlers: function () {
            $("div[data-id='" + this._uId + "']").find("div.data-table-options a.btn-addcompany").on("click", this.addCompany);
            $("div[data-id='" + this._uId + "']").find("div.abm-top-options a.btn-close").on("click", this.close);
        }
    });
    return view;
});